package com.xgkj.diyiketang.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.easemob.easeui.domain.EaseUser;
import com.easemob.easeui.ui.EaseBaseActivity;
import com.easemob.easeui.ui.EaseChatFragment;
import com.xgkj.diyiketang.EatShowApp;
import com.xgkj.diyiketang.data.EatShowUserInfo;
import com.xgkj.diyiketang.http.engine.ShowToastTaskEventListener;
import com.xgkj.diyiketang.hxsdk.DemoHelper;
import com.xgkj.diyiketang.task.GetUserInfoTask;
import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.fragment.ChatFragment;
import com.xgkj.diyiketang.hxsdk.DemoDBManager;
import com.xgkj.diyiketang.util.PalLog;

public class ChatActivity extends EaseBaseActivity {
    private final String TAG = getClass().getSimpleName();
    public static ChatActivity activityInstance;
    private EaseChatFragment chatFragment;
    String toChatUsername;
    String toChatNick;
    String toChatLogo;

    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        setContentView(R.layout.em_activity_chat);
        activityInstance = this;
        //聊天人或群id
        toChatUsername = getIntent().getExtras().getString("userId");
//        PalLog.e("聊天id",toChatUsername);
        toChatNick = getIntent().getExtras().getString("userNick");
        toChatLogo= getIntent().getExtras().getString("fromLogo");
        //保存联系人
        EaseUser user = new EaseUser(toChatUsername);
        user.setNick(toChatNick);
        user.setAvatar(toChatLogo);
        EaseUser cacheUser = DemoDBManager.getInstance().getUser(toChatUsername);
        if (cacheUser != null &&
                ((toChatNick != null && !toChatNick.equals(cacheUser.getNick()))
                        || (toChatLogo != null && !toChatLogo.equals(cacheUser.getAvatar())))) {
            DemoHelper.getInstance().saveContact(user);
        }else if ((cacheUser.getNick() == null || cacheUser.getAvatar() == null) && (toChatNick == null || toChatLogo == null)){
            doGetUserInfo(toChatUsername);
        }
        //可以直接new EaseChatFratFragment使用
        chatFragment = new ChatFragment();
        //传入参数
        chatFragment.setArguments(getIntent().getExtras());

        getSupportFragmentManager().beginTransaction().add(R.id.container, chatFragment).commit();
        
    }

    private void doGetUserInfo(final String from){
        GetUserInfoTask task = new GetUserInfoTask(from);
        task.addListener(new ShowToastTaskEventListener(this){
            @Override
            public void handleTaskCompleted(int taskId, int result, Object data) {
                super.handleTaskCompleted(taskId, result, data);
                if(data != null){
                    EatShowUserInfo info = (EatShowUserInfo) data;
                    if (info != null){
                        String nick = info.getNickName();
                        String userLogo = info.getUserLogo();
                        EaseUser user = new EaseUser(from);
                        user.setNick(nick);
                        user.setAvatar(userLogo);
                        EaseUser cacheUser = DemoDBManager.getInstance().getUser(from);
                        if(cacheUser != null && (!nick.equals(cacheUser.getNick()) || !userLogo.equals(cacheUser.getAvatar()))) {
                            DemoHelper.getInstance().saveContact(user);
                            PalLog.e(TAG, "聊天用户保存成功");
                            ((ChatFragment)chatFragment).refreshList();
                        }else{
                            PalLog.e(TAG,"用户已存在");
                        }
                    }
                }
            }

            @Override
            public void handleTaskError(int taskId, int errorCode, String errorMsg) {
                super.handleTaskError(taskId, errorCode, errorMsg);
            }
        });
        EatShowApp.getInstance().getTaskManager().addTask(task);
    }
    
    @Override
    protected void onDestroy() {
        super.onDestroy();
        activityInstance = null;
    }
    
    @Override
    protected void onNewIntent(Intent intent) {
        // 点击notification bar进入聊天页面，保证只有一个聊天页面
        String username = intent.getStringExtra("userId");
        if (toChatUsername.equals(username))
            super.onNewIntent(intent);
        else {
            finish();
            startActivity(intent);
        }

    }
    @Override
    public void onBackPressed() {
        chatFragment.onBackPressed();
    }
    
    public String getToChatUsername(){
        return toChatUsername;
    }
}
